<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户信息修改页面</title>
</head>
<body>
<div id="app" style=" width:600px; margin:0 auto;border: black solid; position: absolute;
    left:50%;
    top:50%;
    transform: translate(-50%, -50%);padding:50px">
    <h2>完善考生信息信息</h2>
    <h3>一下所有项目均为必填项，请认真填写</h3>
    <form action="index.html" method="post">
        <label>
            <div class="label-text">用户名：</div>
            <input type="text"  name="username" >
        </label>
        <label>
            <div>性别:
                <label><input type="radio" name="sex" value="男生">男生</label>
                <label><input type="radio" name="sex" value="女生">女生</label>
                <a style="font-family: STKaiti">(注：来自实名注册，如果需要修改请前往实名注册修改！)</a>
            </div>

        </label>
        <label>
            <div>所在省市：</div>
            <input type="text">
            <a style="font-family: STKaiti">（注：来自购买会员时所选的省市）</a>
        </label>

        <label>
            <div class="label-text">选课类型：</div>&emsp;
            <input type="checkbox" value="物理" v-model="subjects"/>物理&emsp;
            <input type="checkbox" value="化学" v-model="subjects"/>化学&emsp;
            <input type="checkbox" value="生物" v-model="subjects"/>生物&emsp;
            <input type="checkbox" value="政治" v-model="subjects"/>政治&emsp;
            <input type="checkbox" value="历史" v-model="subjects"/>历史&emsp;
            <input type="checkbox" value="地理" v-model="subjects"/>地理
        </label>
        <label>
            <div class="label-text">所得分数：</div>
            <input type="text"  name="score">
        </label>
        <label>
            <div>位次：</div>
            <input type="text">
        </label>
        <br>
        <div style="padding-top:50px;margin:0 auto; ">
        <button style="background-color: green" type="button" @click="saveUserInfo1">提 交</button>&emsp;
        <a href="index.html">返回首页</a>
        </div>
    </form>
</div>
</body>
</html>

<script src="js/vue.js"></script>
<script src="js/axios.min.js"></script>
<script>
    const app = new Vue({
        el: "#app",
        data:{
            users:{},
            newUser:{},
            src:"",
            subjects:[],
            isExists:{}
        },
        methods:{
            saveUserInfo1(){
                console.log(this.users.name + " " + this.users.password + " " + this.subjects.toString() + " "+ this.users.score);
                if(!this.users.name){
                    alert("用户名不能为空!")
                    return;
                }else if(this.isExists===1){
                    alert("该用户名已经存在!")
                    return;
                }
                if(!this.users.password){
                    alert("密码不能为空!")
                    return;
                }
                if(this.subjects.length!==3){
                    console.log(this.users.length)
                    alert("选课类型数目必须为3!")
                    return;
                }
                if(this.users.score>750 || this.users.score<0){
                    alert("分数值异常, 请输入你正确的分数!")
                    return;
                }
                axios.post('http://localhost:8080/users/updateUsers', {
                    id: this.users.id,
                    name: this.users.name,
                    password: this.users.password,
                    type: this.subjects.toString(),
                    score: this.users.score
                }) .then(function (response) {
                    console.log(response);
                    alert("修改用户信息成功! 点击返回主页-->");
                    location.href='index.html';
                })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            findUserById(id){
                axios.get("http://localhost:8080/users/getUsersById?id="+id).then((res)=>{
                    console.log(res.data)
                    this.users = res.data;
                });
            },
            toAdmin(){
                if(window.localStorage.getItem("role") === 'administrator') location.href='admin/user.html';
                else alert("非管理员 无授权进入管理员界面!")
            }
        },
        created(){
            if(0){
                window.location='login.html';
            }else {
                console.log(window.localStorage.getItem("userid").toString());
                this.findUserById(window.localStorage.getItem("userid").toString());
            }
        }
    });
</script>